package cn.ithers.blog.controller.admin;

import cn.ithers.blog.annotion.CheckTooFrequentCommit;
import cn.ithers.blog.dto.FriendLinkDTO;
import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.IFriendLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhanghuan
 * @date: 2020/8/28 14:06
 * @description:
 */
@RestController
@Api(value = "友情链接", tags = "友情链接")
@RequestMapping("/admin/friend_link")
public class FriendLinkAdminApi {

    @Autowired
    private IFriendLinkService friendLinkService;

    /**
     * 添加友情链接
     *
     * @param friendLinkDTO
     * @return
     */
    @CheckTooFrequentCommit
    @ApiOperation(value = "添加友情链接")
    @PreAuthorize("@permission.admin()")
    @PostMapping()
    public ResponseResult addFriendsLink(@RequestBody FriendLinkDTO friendLinkDTO) {
        return friendLinkService.addFriendLink(friendLinkDTO);
    }

    /**
     * 删除友情链接
     *
     * @param friendLinkId
     * @return
     */
    @ApiOperation(value = "删除友情链接")
    @PreAuthorize("@permission.admin()")
    @DeleteMapping("/{friendLinkId}")
    public ResponseResult deleteFriendLink(@ApiParam(value = "友链ID", required = true) @PathVariable("friendLinkId") String friendLinkId) {
        return friendLinkService.deleteFriendLink(friendLinkId);
    }

    /**
     * 更新友情链接
     *
     * @param friendLinkId
     * @return
     */
    @CheckTooFrequentCommit
    @ApiOperation(value = "更新友情链接")
    @PreAuthorize("@permission.admin()")
    @PutMapping("/{friendLinkId}")
    public ResponseResult updateFriendLink(@ApiParam(value = "友链ID", required = true) @PathVariable("friendLinkId") String friendLinkId,
                                           @RequestBody FriendLinkDTO friendLinkDTO) {
        return friendLinkService.updateFriendLink(friendLinkId, friendLinkDTO);
    }

    /**
     * 获取友情链接
     *
     * @param friendLinkId
     * @return
     */
    @ApiOperation(value = "获取友情链接")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/{friendLinkId}")
    public ResponseResult getFriendLink(@ApiParam(value = "友链ID", required = true) @PathVariable("friendLinkId") String friendLinkId) {
        return friendLinkService.getFriendLink(friendLinkId);
    }

    /**
     * 获取友情链接列表list
     *
     * @return
     */
    @ApiOperation(value = "获取友情链接列表list")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/list")
    public ResponseResult getFriendLinkList() {
        return friendLinkService.getFriendLinkList();
    }
}
